/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.tt.cards.User.boundary;

import com.tt.cards.User.controller.WuserEJB;
import com.tt.cards.User.entity.Wuser;
import com.tt.cards.User.entity.WuserRole;
import java.io.Serializable;
import java.util.List;
import java.util.Arrays;
import javax.ejb.EJB;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.RequestScoped;
import javax.faces.context.FacesContext;

/**
 *
 * @author ylevkovych
 */
@ManagedBean
@RequestScoped
public class WuserBoundary implements Serializable {
    
    private static final long serialVersionUID = 1L;
    
    private Wuser wuser = null;
    private List<Wuser> wuserList = null;
    
    private String password = "";
    private String confirm = "";
       
    @EJB
    private WuserEJB wuserEJB;
    
    public WuserBoundary() {    
        wuser = new Wuser();
    }
    
    public String doCreateNewUser() {
        return "/pages/user/new.faces";
    }
    
    public String doListWuser() {
        wuserList = wuserEJB.findAll();
        return "/pages/user/list.faces";
    }
    
    public String doExitToMain() {
        try {
            return "/pages/welcome.faces";
        } catch (Exception e) {
            return null;
        }        
    }
    
    public List<WuserRole> wuserRolesValues() {
        List<WuserRole> list = Arrays.asList(WuserRole.values());
        return list;
    }

    public String saveWuser() {
        FacesContext fctx = FacesContext.getCurrentInstance();
        
        try {
            if (password == null) {
                fctx.addMessage("newUserForm:passwordPanel", new FacesMessage(FacesMessage.SEVERITY_ERROR, 
                    "Пароль не задан", 
                    "Пароль не задан"));
                return null;
            }
                                
            if (!password.equals(confirm)) {
                fctx.addMessage("newUserForm:passwordPanel", new FacesMessage(FacesMessage.SEVERITY_ERROR, 
                    "Подтверждение не совпадает с паролем", 
                    "Подтверждение не совпадает с паролем"));
                return null;
            }
            wuserEJB.create(wuser);
            return "/pages/user/list.faces";
        } catch (Exception e) {
            fctx.addMessage("newUserForm:name", new FacesMessage(FacesMessage.SEVERITY_ERROR, 
                    "Пользователь не создан", 
                    "Пользователь не создан"));
            System.out.println("[new.xml] "+e.getMessage());
            return null;
        }
    }
    
    public String txtCompanyByDefault(){
        if(wuser == null) {
            return "Значение не выбрано";
        } else if (wuser.getCompanyByDefault() == null) {
            return "Пользователь не создан";
        } else {
            return wuser.getCompanyByDefault().toString();
        }
    }
    
    public String txtWarehouseByDefault() {
        if(wuser == null) {
            return "Пользователь не создан";
        } else if (wuser.getWarehouseByDefault() == null) {
            return "Пользователь не создан";
        } else {
            return wuser.getWarehouseByDefault().toString();
        }
    }
    
    public String txtCurrencyByDefault() {
        if(wuser == null) {
            return "Пользователь не создан";
        } else if (wuser.getCurrencyByDefault() == null) {
            return "Пользователь не создан";
        } else {
            return wuser.getCurrencyByDefault().toString();
        }
    }
        
    public List<Wuser> getWuserList() {
        return wuserList;
    }

    public void setWuserList(List<Wuser> wuserList) {
        this.wuserList = wuserList;
    }
    
    public Wuser getWuser() {
        return wuser;
    }

    public void setWuser(Wuser wuser) {
        this.wuser = wuser;
    }

    public String getConfirm() {
        return confirm;
    }

    public void setConfirm(String confirm) {
        this.confirm = confirm;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }
        
}
